﻿#include "BrowserTableHandlerMaterialFiber.h"
#include "OperatorsInterface/OperAbstractMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsFiber.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

void GUI::BrowserTableHandlerMaterialFiber::updateTable()
{
    if (_tableWidget == nullptr) return;
    _tableWidget->clear();
    _tableWidget->setColumnCount(0);
    _tableWidget->setRowCount(0);

//设置表头
    QStringList headerLabels;
    headerLabels << tr("Name") << tr("Density [kg/m3]") << tr("Flow Resistivity [N.s/m4]") << tr("Notes");
    _tableWidget->setColumnCount(headerLabels.size());
    _tableWidget->setHorizontalHeaderLabels(headerLabels);
//更新表格
    this->updateData();
}

void GUI::BrowserTableHandlerMaterialFiber::updateData()
{
    if (_tableWidget == nullptr) return;
    Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
    if (phys == nullptr) return;
//获取材料管理器
    Acoustics::FITKAcousticsMaterialManager* matmgr = phys->getMaterialManager();
    if (matmgr == nullptr) return;

    QList<Acoustics::FITKAcousticsAbsMaterial*> matList = matmgr->getAcousticsMaterials(
        Acoustics::FITKAcousticsAbsMaterial::Fiber);
//材料数量
    const int n = matList.size();
    _tableWidget->setRowCount(n);

    for (int i = 0; i < n; ++i)
    {
    //转换为各向同性固体材料
        Acoustics::FITKAcousticsMaterialsFiber* maf = dynamic_cast<Acoustics::FITKAcousticsMaterialsFiber*>
            (matList.at(i));
        if (maf == nullptr) continue;
    //数据ID
        const int id = maf->getDataObjectID();
    //填充表格，用type表示ID
        _tableWidget->setItem(i, 0, new QTableWidgetItem(maf->getDataObjectName(), id));
        _tableWidget->setItem(i, 1, new QTableWidgetItem(QString::number(maf->getDensity()), id));
        _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(maf->getFlowResistivity()), id));
        _tableWidget->setItem(i, 3, new QTableWidgetItem(maf->getMaterialDescribe(), id));
    }
    _tableWidget->autoResizeCellWidth();
    _tableWidget->setTableItemEditable(false);
}

EventOper::OperAbstractMaterial * GUI::BrowserTableHandlerMaterialFiber::getMaterialOper()
{
//从操作器仓库获取
    return Core::FITKOperatorRepo::getInstance()->
        getOperatorT<EventOper::OperAbstractMaterial>("OperMaterialFiber");
}

bool GUI::BrowserTableHandlerMaterialFiber::enableAttriSet()
{
    QList<QTableWidgetItem*> selectItems = this->getSelectItems();
    if (selectItems.size() < 2) return false;

    QTableWidgetItem* item = selectItems.at(0);
    if (item == nullptr) return false;
    //名称列
    const int colIndex = item->column();
    //只有密度能批量
    if (colIndex != 1) return false;

    return true;
}
